using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using SHDocVw;

namespace Thinxoft.XedusLive
{
	/// <summary>
	/// Summary description for StartForm.
	/// </summary>
	public class StartForm : System.Windows.Forms.Form
	{
        private System.Windows.Forms.NotifyIcon notifyIcon;
        private System.ComponentModel.IContainer components;
        //p2p.P2PWebService pws = new p2p.P2PWebService();
        private MenuItem menuItem4;
        private MenuItem btnAdmin;
        private MenuItem btnShowInfo;
        private MenuItem menuItem3;
        private MenuItem btnRegister;
        private MenuItem btnConnect;
        private MenuItem menuItem1;
        private MenuItem menuItem2;
        private MenuItem btnDefault;
        private MenuItem menuItem5;
        private MenuItem btnKill;
        private ContextMenu contextMenu;
        private Panel panel1;
        private Label lblMessage;
        private Panel panel2;
        XedusMenu options;

        public StartForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
            options = new XedusMenu();
               

            this.notifyIcon.Click += delegate(object sender, EventArgs args)
            {
                if (options != null)
                {
                    ShowMenu();
                }
            };
        }

        void ShowMenu()
        {
            //options = new XedusMenu();
            //options.panel1.BackgroundImage = Properties.Resources.xedusMenu2;
            //options.FormClosed += delegate(object sender, FormClosedEventArgs args)
            //{
            //    options = null;
            //};
            int top = Screen.PrimaryScreen.WorkingArea.Bottom - options.Height;
            int left = Screen.PrimaryScreen.WorkingArea.Right - options.Width;
            options.Top = top;
            options.Left = left;
            options.Show();
        }

        internal void ShowStartupMessage(string message)
        {
            lblMessage.Text = message;
        }

        /// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(StartForm));
            this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.btnAdmin = new System.Windows.Forms.MenuItem();
            this.btnShowInfo = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.btnRegister = new System.Windows.Forms.MenuItem();
            this.btnConnect = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.btnDefault = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.btnKill = new System.Windows.Forms.MenuItem();
            this.contextMenu = new System.Windows.Forms.ContextMenu();
            this.panel1 = new System.Windows.Forms.Panel();
            this.lblMessage = new System.Windows.Forms.Label();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // notifyIcon
            // 
            this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon")));
            this.notifyIcon.Text = "Xedus Server";
            this.notifyIcon.Visible = true;
            // 
            // menuItem4
            // 
            this.menuItem4.Enabled = false;
            this.menuItem4.Index = 0;
            this.menuItem4.Text = "XEDUS  SERVER 1.2";
            // 
            // btnAdmin
            // 
            this.btnAdmin.Index = 0;
            this.btnAdmin.Text = "Launch Admin";
            this.btnAdmin.Click += new System.EventHandler(this.btnAdmin_Click);
            // 
            // btnShowInfo
            // 
            this.btnShowInfo.Index = 1;
            this.btnShowInfo.Text = "Show Xedus Info";
            this.btnShowInfo.Click += new System.EventHandler(this.btnShowInfo_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 1;
            this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.btnAdmin,
            this.btnShowInfo});
            this.menuItem3.Text = "Administration";
            // 
            // btnRegister
            // 
            this.btnRegister.Index = 0;
            this.btnRegister.Text = "Register";
            this.btnRegister.Click += new System.EventHandler(this.btnRegister_Click);
            // 
            // btnConnect
            // 
            this.btnConnect.Index = 1;
            this.btnConnect.Text = "Connect";
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 2;
            this.menuItem1.Text = "Disconnect";
            this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 2;
            this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.btnRegister,
            this.btnConnect,
            this.menuItem1});
            this.menuItem2.Text = "Peer  to Peer Server";
            // 
            // btnDefault
            // 
            this.btnDefault.Index = 3;
            this.btnDefault.Text = "Sample Site";
            this.btnDefault.Click += new System.EventHandler(this.btnDefault_Click);
            // 
            // menuItem5
            // 
            this.menuItem5.Index = 4;
            this.menuItem5.Text = "X-File Help";
            this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
            // 
            // btnKill
            // 
            this.btnKill.Index = 5;
            this.btnKill.Text = "Kill Server";
            this.btnKill.Click += new System.EventHandler(this.btnKill_Click);
            // 
            // contextMenu
            // 
            this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem4,
            this.menuItem3,
            this.menuItem2,
            this.btnDefault,
            this.menuItem5,
            this.btnKill});
            // 
            // panel1
            // 
            this.panel1.BackgroundImage = global::Thinxoft.XedusLive.Properties.Resources.sphere_and_tiles;
            this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.panel2);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(400, 300);
            this.panel1.TabIndex = 0;
            // 
            // lblMessage
            // 
            this.lblMessage.BackColor = System.Drawing.Color.Transparent;
            this.lblMessage.Location = new System.Drawing.Point(3, 6);
            this.lblMessage.Name = "lblMessage";
            this.lblMessage.Size = new System.Drawing.Size(392, 23);
            this.lblMessage.TabIndex = 0;
            this.lblMessage.Text = "label1";
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.lblMessage);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 269);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(398, 29);
            this.panel2.TabIndex = 1;
            // 
            // StartForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackgroundImage = global::Thinxoft.XedusLive.Properties.Resources.sphere_and_tiles;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(400, 300);
            this.Controls.Add(this.panel1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximumSize = new System.Drawing.Size(400, 300);
            this.Name = "StartForm";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "StartForm";
            this.TopMost = true;
            this.panel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);

        }
		#endregion

		internal void btnKill_Click(object sender, System.EventArgs e)
		{
			//if connected disconnect before exiting Xedus
			if(Start.Connected)
			{
                //AsyncCallback acb = new AsyncCallback(delegate(IAsyncResult res)
                //{
                //    pws.EndDisconnectFromNOW(res);
                //    MessageBox.Show("disconected");
                //    Start.XedusState.XedusSettings.SaveSettings();
                //    Application.Exit();
                //});
                //pws.BeginDisconnectFromNOW(Start.Email,acb,null);
			}
			//if not just save settings and exit
			Start.XedusState.XedusSettings.SaveSettings();
            Start.KillAll();		
		}    
       
		private void btnAdmin_Click(object sender, System.EventArgs e)
		{
			Start.AdminUI.Show();
		}

		private void btnShowInfo_Click(object sender, System.EventArgs e)
		{
			Start.XedusInfoUI.DisplayInfo();
		}		

		internal void btnConnect_Click(object sender, System.EventArgs e)
		{
            //this.btnConnect.Enabled = false;
            //ConnectToNowForm ctn = new ConnectToNowForm();
            //ctn.Closed += delegate(object obj, EventArgs ags)
            //{
            //    this.btnConnect.Enabled = true;
            //};
            //ctn.Show();
		}

		internal void btnRegister_Click(object sender, System.EventArgs e)
		{
            Start.AdminUI.ShowRegistration();
        }

		internal void menuItem1_Click(object sender, System.EventArgs e)
		{
            try
            {
                //AsyncCallback acb = new AsyncCallback(
                //   delegate(IAsyncResult res)
                //   {
                //       pws.EndDisconnectFromNOW(res);
                //   });
                //pws.BeginDisconnectFromNOW(Start.Email, acb, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("could not disconnect from NOW");
            }
		}		

		internal void btnDefault_Click(object sender, System.EventArgs e)
		{			
			System.Diagnostics.Process.Start("iexplore","http://localhost:4274/samples/testsite/tests.x");
		}

		internal void menuItem5_Click(object sender, System.EventArgs e)
		{			
			System.Diagnostics.Process.Start("iexplore","http://localhost:4274/samples/html/x-intro.htm");
        }

        private void panel_Paint(object sender, PaintEventArgs e)
        {

        }

        
	}
}
